import aes from 'crypto-js/aes';
import CryptoJS from 'crypto-js';
import secret from '../../../../../utils/secret';

const allRows = [
    {
        id: '11',
        name: '张三',
        employeeNo: '860001',
        cardNo: '362031199010241238',
        startDate: '2020-11-01',
        endDate: null,
        status: '',
        departmentName: '',
        housePlace: '',
        socialStartYear: 2019,
        socialStartMonth: 10,
        encryptOldBaseAmt: '',
        encryptUnemployBaseAmt: '',
        encryptHospitalBaseAmt: '',
        encryptFundBaseAmt: '',
        standardYlbx: '',
        standardSybx: '',
        standardYlbx2: '',
        standardGjj: '',
        zjyzt: '已增员'
    },
    {
        id: '12',
        name: '李四',
        employeeNo: '860002',
        cardNo: '362031199010241239',
        startDate: '2020-10-29',
        endDate: '2020-11-30',
        status: '入职',
        departmentName: '微微科技部',
        housePlace: '厦门-户籍本地',
        socialStartYear: 2019,
        socialStartMonth: 10,
        encryptOldBaseAmt: '16680',
        encryptUnemployBaseAmt: '1700',
        encryptHospitalBaseAmt: '15500',
        standardYlbx: 1234.5,
        standardSybx: 2234.5,
        standardYlbx2: 3234.5,
        standardGjj: 4234.5,
        encryptFundBaseAmt: '12800',
        zjyzt: '已增员'
    },
    {
        id: '13',
        name: '王五',
        employeeNo: '860003',
        cardNo: '362031199010241233',
        startDate: '2020-01-01',
        endDate: '2020-10-10',
        status: '在职',
        departmentName: '微微科技部',
        housePlace: '待增员',
        socialStartYear: 2019,
        socialStartMonth: 10,
        encryptOldBaseAmt: '16680',
        encryptUnemployBaseAmt: '1700',
        encryptHospitalBaseAmt: '15500',
        encryptFundBaseAmt: '12800',
        standardYlbx: '16680',
        standardSybx: '1700',
        standardYlbx2: '15500',
        standardGjj: '12800',
        zjyzt: '已增员'
    },
    {
        id: '14',
        name: '欧阳世家',
        employeeNo: '860004',
        cardNo: '362031199010241234',
        startDate: '2020-11-01',
        endDate: '2020-9-10',
        status: '在职',
        departmentName: '微微科技部',
        housePlace: '待增员',
        socialStartYear: 2019,
        socialStartMonth: 10,
        encryptOldBaseAmt: '16680',
        encryptUnemployBaseAmt: '1700',
        encryptHospitalBaseAmt: '15500',
        encryptFundBaseAmt: '12800',
        standardYlbx: '16680',
        standardSybx: '1700',
        standardYlbx2: '15500',
        standardGjj: '12800',
        zjyzt: '已增员'
    },
    {
        id: '15',
        name: '欧阳磊',
        employeeNo: '860004',
        cardNo: '362031199010241234',
        startDate: '2020-11-01',
        endDate: '2020-9-10',
        status: '在职',
        departmentName: '微微科技部',
        housePlace: '待增员',
        socialStartYear: 2019,
        socialStartMonth: 10,
        encryptOldBaseAmt: '16680',
        encryptUnemployBaseAmt: '1700',
        encryptHospitalBaseAmt: '15500',
        encryptFundBaseAmt: '12800',
        standardYlbx: '16680',
        standardSybx: '1700',
        standardYlbx2: '15500',
        standardGjj: '12800',
        zjyzt: '已增员'
    }
];

const getDecrypt = (params, secrets) =>
    JSON.parse(
        aes
            .decrypt(params, CryptoJS.enc.Utf8.parse(secrets), {
                iv: CryptoJS.enc.Utf8.parse(secrets),
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            })
            .toString(CryptoJS.enc.Utf8)
    );

const search = request => {
    const params = getDecrypt(request.body.params, secret);

    const startIndex = (params.pageNo - 1) * params.pageSize;
    const endIndex = params.pageNo * params.pageSize;
    const pages = Math.ceil(allRows.length / params.pageSize);
    console.log('params:', params);
    console.log('startIndex:', startIndex);
    console.log('endIndex:', endIndex);
    const rows = allRows.length <= startIndex ? null : allRows.slice(startIndex, endIndex);
    console.log('rows:', rows);

    return {
        code: 10000,
        data: {
            total: allRows.length,
            pages,
            pageNo: params.pageNo,
            pageSize: params.pageSize,
            records: rows
        }
    };
};

const list = () => ({
    code: 10000,
    data: [
        {
            id: '1',
            socialCityCode: 'a',
            socialCityName: '厦门'
        },
        {
            id: '2',
            socialCityCode: 'b',
            socialCityName: '福州'
        }
    ]
});

const detail = () => ({
    code: 10000,
    data: [
        {
            id: '01',
            housePlace: 1,
            socialType: 1,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 14.6,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '02',
            housePlace: 1,
            socialType: 2,
            minPayAmt: 2800,
            companyRate: 22,
            companyAmt: 216,
            personRate: 28,
            personAmt: 146,
            totalAmt: 760,
            limitDownAmt: 26168,
            limitUpperAmt: 2800
        },
        {
            id: '03',
            housePlace: 1,
            socialType: 3,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '04',
            housePlace: 1,
            socialType: 4,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '05',
            housePlace: 1,
            socialType: 5,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '06',
            housePlace: 1,
            socialType: 6,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },

        {
            id: '07',
            housePlace: 2,
            socialType: 1,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '08',
            housePlace: 2,
            socialType: 2,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '09',
            housePlace: 2,
            socialType: 3,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '10',
            housePlace: 2,
            socialType: 4,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '11',
            housePlace: 2,
            socialType: 5,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 16168,
            limitUpperAmt: 1800
        },
        {
            id: '12',
            housePlace: 2,
            socialType: 6,
            minPayAmt: 1800,
            companyRate: 12,
            companyAmt: 216,
            personRate: 8,
            personAmt: 146,
            totalAmt: 360,
            limitDownAmt: 168,
            limitUpperAmt: 1800
        }
    ]
});

const saveEditDetail = (req, res) =>
    setTimeout(() => {
        res.json({ code: 10000, data: [] });
    }, 2000);

const deleteDetail = (req, res) =>
    setTimeout(() => {
        res.json({ code: 10000, data: [] });
    }, 2000);

export default {
    'POST x/api/finance/salary/v1/setting/social/employee/page': (req, res) => {
        res.json(search(req));
    },
    'GET x/api/finance/salary/v1/setting/social/list/*': (req, res) => {
        res.json(list(req));
    },
    'GET x/api/finance/salary/v1/setting/social/detial/*/*': (req, res) => {
        res.json(detail(req));
    },
    'PUT x/api/finance/salary/v1/setting/social/detail': (req, res) => {
        saveEditDetail(req, res);
    },
    'POST x/api/finance/salary/v1/setting/social/detail': (req, res) => {
        saveEditDetail(req, res);
    },
    'DELETE x/api/finance/salary/v1/setting/social/detail/*': (req, res) => {
        deleteDetail(req, res);
    }

};
